#!/usr/bin/env python
import pygtk
pygtk.require('2.0')

import gtk
import sys
import os
import string
import random

alist = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]


class Deducto():
			
	def __init__(self, handle = None, is_stand_alone = False,base_path = ""):
		self.is_stand_alone = is_stand_alone
		
		path = __file__
		path = path.rpartition("/")
		
		self.load_activity_interface(base_path)
		gtk.main()


	def load_activity_interface(self,basepath):
				
		self.base_path = basepath
	
		# Initiating the game parameters
		self.blackimage = os.path.join(self.base_path, "0.jpg")
	        self.whiteimage = os.path.join(self.base_path, "1.jpg")
	        
		self.level = 1
	        self.correct = 0
	        self.playStatus = 0
	        self.pauseStatus = 0
	        self.boardStatus = 1
		
	        self.start = 0
	        self.tot = 0.0000
	        self.cor = 0.0000
		
	        self.temparray = alist	        


		if self.is_stand_alone:
			self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
			self.main_window.connect("destroy", self.delete_event, None)
			self.main_window.set_border_width(10)
			self.main_window.set_title("Deducto")
		
		if not self.is_stand_alone:
			toolbox = activity.ActivityToolbox(self)			
			self.set_toolbox(toolbox)
			toolbox.show_all()
			# Set title for our Activity
			self.set_title('Deducto')

		self.Vcontainer   = gtk.VBox()
		self.Vcontainer.show()
		
		if self.is_stand_alone:
			self.main_window.add(self.Vcontainer)
		
		self.vcontainer_left = gtk.VBox()
		self.vcontainer_left.set_border_width(10)
		self.vcontainer_left.show()

		self.vcontainer_right = gtk.VBox()
		self.vcontainer_right.set_border_width(10)
		self.vcontainer_right.show()

		self.Hcontainer1 = gtk.HBox()
		self.Hcontainer1.set_border_width(10)
		self.Hcontainer1.show()

		self.Hcontainer2 = gtk.HBox()
		self.Hcontainer2.set_border_width(10)
		self.Hcontainer2.show()

		self.Hcontainer3 = gtk.HBox()
		self.Hcontainer3.set_border_width(10)
		self.Hcontainer3.show()

		self.Hcontainer3.pack_start(self.vcontainer_left,True, True, 0)
		self.Hcontainer3.pack_start(self.vcontainer_right,True, True, 0)

		self.Hcontainer4 = gtk.HBox()
		self.Hcontainer4.set_border_width(10)
		self.Hcontainer4.show()
		

		self.Vcontainer.pack_start(self.Hcontainer1,True, True,0)
		self.Vcontainer.pack_start(self.Hcontainer2,False, False,0)
		self.Vcontainer.pack_start(self.Hcontainer3,True, True,0)
		self.Vcontainer.pack_start(self.Hcontainer4,False, False,0)


		self.new_game_button = gtk.Button(label="New Game")
		self.new_game_button.show()
		self.new_game_button.connect("clicked", self.menuplay, None)

		self.save_button = gtk.Button(label="Save")
		self.save_button.show()
		self.save_button.connect("clicked", self.save, None)

		self.open_button = gtk.Button(label="Open")
		self.open_button.show()
		self.open_button.connect("clicked", self.saved, None)

		self.how_button = gtk.Button(label="How to Play")
		self.how_button.show()
		self.how_button.connect("clicked", self.how_box, None)

		self.about_button = gtk.Button(label="About")
		self.about_button.show()
		self.about_button.connect("clicked", self.about_box, None)

		self.Hcontainer1.pack_start(self.new_game_button, True, True )
		self.Hcontainer1.pack_start(self.save_button,True, True )
		self.Hcontainer1.pack_start(self.open_button, True, True )
		self.Hcontainer1.pack_start(self.how_button, True, True )
		self.Hcontainer1.pack_start(self.about_button, True, True )

		self.resume_button = gtk.Button(label="Resume")
		self.resume_button.show()
		self.resume_button.connect("clicked", self.resume, None)

		self.stats_button = gtk.Button(label="Statistics")
		self.stats_button.show()
		self.stats_button.connect("clicked", self.stats, None)

		self.sample_button = gtk.Button(label="Sample")
		self.sample_button.show()
		self.sample_button.connect("clicked", self.sample, None)

		self.quit_button = gtk.Button(label="Quit")
		self.quit_button.show()
		self.quit_button.connect("clicked", self.delete_event, None)

		self.Hcontainer2.pack_start(self.resume_button, True, True )
		self.Hcontainer2.pack_start(self.stats_button,True, True )
		self.Hcontainer2.pack_start(self.sample_button, True, True )
		self.Hcontainer2.pack_start(self.quit_button, True, True )


		# Generating First V-Box i.e. Vcontainer_right containing Stats
		
		self.deducto_label = gtk.Label("DEDUCTO")	
	        self.separator0 = gtk.HSeparator()
   	        self.separator0.show()
		
	 	self.efficiency_label = gtk.Label("Efficiency")
		self.efficiency_value = gtk.Label("?")
	        self.separator1 = gtk.HSeparator()
   	        self.separator1.show()
		
		self.level_label = gtk.Label("Level")
		self.level_value = gtk.Label("?")
	        self.separator2 = gtk.HSeparator()
   	        self.separator2.show()
		
		self.correct_label = gtk.Label("Correct")
		self.correct_value = gtk.Label("?")
		self.separator3 = gtk.HSeparator()
   	        self.separator3.show()
		
		self.welcome1_label = gtk.Label("Welcome")
		self.welcome2_label = gtk.Label("To Deducto")
	        self.separator4 = gtk.HSeparator()
   	        self.separator4.show()
		
			# Packing stats into V-container_right
   	        self.vcontainer_right.pack_start(self.deducto_label, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.separator0, False, True, 5)		
   	        
		self.vcontainer_right.pack_start(self.efficiency_label, False, True, 5)	
   	        self.vcontainer_right.pack_start(self.efficiency_value, False, True, 5)			
   	        self.vcontainer_right.pack_start(self.separator1, False, True, 5)		
		
   	        self.vcontainer_right.pack_start(self.level_label, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.level_value, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.separator2, False, True, 5)		
		
   	        self.vcontainer_right.pack_start(self.correct_label, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.correct_value, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.separator3, False, True, 5)		
		
   	        self.vcontainer_right.pack_start(self.welcome1_label, False, True, 5)		
   	        self.vcontainer_right.pack_start(self.welcome2_label, False, True, 5)	
   	        self.vcontainer_right.pack_start(self.separator4, False, True, 5)			
		self.vcontainer_right.show_all()

			#Generating Table for V-container_left
		self.display_board(alist)
		self.update_board(alist)
		self.frame = gtk.Frame()
		self.frame.add(self.vbox)
		self.frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
		self.frame.show()
		

		# Packing V-container Right and Left into Hcontainer 3
		self.vcontainer_left.pack_start(self.frame, False, False, 0)
		

		# Creating "Yes" & "No" Buttons for Hcontainer 4
		self.button1 = gtk.Button("Yes")
		self.button1.set_size_request(100,35)
		self.button1.connect("clicked", self.opt1)
		self.button1.show()

		self.button2 = gtk.Button("No")
		self.button2.set_size_request(100,35)
		self.button2.connect("clicked", self.opt2)
		self.button2.show()
		
		self.Hcontainer4.pack_start(self.button1,True,False,0)
		self.Hcontainer4.pack_start(self.button2,True,False,0)

		if self.is_stand_alone:
			self.main_window.show()
		else:
			self.set_canvas(self.Vcontainer)
			self.show_all()



	##########################    CALLBACKS    ############################
	
	def delete_event(self, widget, event, data = None) :
		print "Delete_event called......"
		gtk.main_quit()

		
	def save(self, widget, data = None) :
		array1=[0,0,0,0,0,100,100]
	        array1[0]=self.playStatus
        	array1[1]=self.correct
        	array1[2]=self.level
		if self.cor<10:
		        array1[3] = 1
		elif self.cor<100:
		        array1[3] = 2
		else:
		        array1[3] = 3

		if self.tot<10:
		        array1[4] = 1
		elif self.tot<100:
		        array1[4] = 2
		else:
		        array1[4] = 3  
		array1[5]=self.cor
		array1[6]=self.tot

		self.playstatus=0

		if (sys.platform == "win32"):
		        out_file = open("C:\dedu.sav","w")
		        out_file.write(str(array1))
		        out_file.close()
		        flag=1
		else:
		        out_file = open("/tmp/dedu.sav","w")
		        out_file.write(str(array1))
		        out_file.close()
		        flag=1

		self.message("Game","Saved")

	        
	def saved(self,widget,data = None):		#for opening already saved game
	        if (sys.platform == "win32"):
	            in_file = open("C:\dedu.sav","r")
	            details = in_file.read()
	            in_file.close()
	        else:
	            in_file = open("/tmp/dedu.sav","r")
	            details = in_file.read()
	            in_file.close()

	        self.playStatus=int(details[1:2])
	        self.correct=int(details[4:5])
	        self.level=int(details[7:8])
	        a=int(details[10:11])
	        b=int(details[13:14])
	        self.cor=int(details[16:16+a])
	        self.tot=int(details[18+a:18+a+b])

	        self.score(str(100*(self.cor)/(self.tot)))
	        self.pauseStatus=0
	        self.playStatus=1
	        self.start=1
	        self.message("Game","Resumed")

		self.button1.set_label("Yes")
		self.button2.set_label("No")

	        self.updatepanel()
		if( random.randrange( 1, 100 ) < 50 ):
        	    boardArray = self.genBoard(0)
        	    self.boardStatus = 0
        	else:
        	    boardArray = self.genBoard(1)
        	    self.boardStatus = 1	
		self.update_board(boardArray)

	def welcome(self, widget, data = None) :
		dialog = gtk.MessageDialog(
		    parent         = None,
		    flags          = gtk.DIALOG_DESTROY_WITH_PARENT,
		    type           = gtk.MESSAGE_INFO,
		    buttons        = gtk.BUTTONS_OK,
		    message_format = '\t   ' + "Welcome" + '\n\n' + "To the new world of puzzles" + '\n\n' + '\t  ' + "DEDUCTO")

		dialog.set_title('Welcome')
		dialog.connect('response', lambda dialog, response: dialog.destroy())
		dialog.show()


	def how_box(self, widget, data = None) :
		dialog = gtk.MessageDialog(
		    parent         = None,
		    flags          = gtk.DIALOG_DESTROY_WITH_PARENT,
		    type           = gtk.MESSAGE_INFO,
		    buttons        = gtk.BUTTONS_OK,
		    message_format = "Deducto is pattern matching game." + '\n' + "The game encompasses 10 levels each having a unique logic.You would be given a board and you are required to identify the logic of the level." + '\n' + "If the boxes in the board are arranged according to the logic then it is a TRUE board.If the boxes are arranged opposite to the logic, then it is a FALSE board." + '\n' + "While playing, you have to give the answer as YES or NO depending upon whether he finds the board to be TRUE or FALSE." + '\n' + "For being promoted to the next level, you need to give five consecutive correct answers." + '\n' + "There are innumerable examples for every level, for assisting you to identify the logic. A click on SAMPLE TAB produces an example.A click on TRUE tab at the bottom of the screen displays a True board and FALSE tab produces a False Board.RESUME TAB takes you back to game." + '\n' + "ENJOY!")

		dialog.set_title('How to play?')
		dialog.connect('response', lambda dialog, response: dialog.destroy())
		dialog.show()


	def about_box(self, widget, data = None) :
		dialog = gtk.MessageDialog(
		    parent         = None,
		    flags          = gtk.DIALOG_DESTROY_WITH_PARENT,
		    type           = gtk.MESSAGE_INFO,
		    buttons        = gtk.BUTTONS_OK,
		    message_format = '\t\t\t' + "DEDUCTO" + '\n\n' + "Written in Python by Open Source Community, NSIT")

		dialog.set_title('About Deducto')
		dialog.connect('response', lambda dialog, response: dialog.destroy())
		dialog.show()

	def stats(self, widget, data = None) :
		
		# To prevent Division by Zero Error
		if(self.tot == 0) :
			tot = 1
		else :
			tot = self.tot

		dialog = gtk.MessageDialog(
		    parent         = None,
		    flags          = gtk.DIALOG_DESTROY_WITH_PARENT,
		    type           = gtk.MESSAGE_INFO,
		    buttons        = gtk.BUTTONS_OK,
		    message_format = "Correctly Answered : " + str(self.cor) + '\n\n' + "Total Answered : " + str(self.tot) + '\n\n' + "Percentage Efficiency : " + str(str(100*(self.cor)/(tot))))

		dialog.set_title('STATISTICS!')
		dialog.connect('response', lambda dialog, response: dialog.destroy())
		dialog.show()
   		
		
	def display_board(self,boardArray):
	        array = boardArray
		
		# Creating a list containing 5-Hboxes and 6-Hseparators for full board
		self.hbox_list = []
		
		for i in range(0,11,1) :
			if(i%2 == 1) :
				self.a = gtk.HBox(False,0)
				self.hbox_list += [self.a]
			else :
				self.b = gtk.HSeparator()
				self.hbox_list += [self.b]
		
		self.vbox = gtk.VBox(False,0)
        	for i in range(0,5,1):
        	    for j in range(0,5,1):
        	        k = (i*5 + j)
        	        if( array[k] == 0):
        	            image_filename = self.blackimage
        	        else:
        	            image_filename = self.whiteimage
		
			self.image = gtk.Image()
			self.image.set_from_file(image_filename)
			vseparator = gtk.VSeparator()
			vseparator.show()
		
			self.hbox_list[2*i+1].pack_start(self.image, False, False, 6)		# adding image in H-box
			self.hbox_list[2*i+1].pack_start(vseparator, False, False, 0)		# adding v-separator in H-box
		
		    self.hbox_list[2*i].show()							#this shows H-separators of the V-box
		    self.hbox_list[2*i+1].show()						#this shows H-boxes of the V-box
		
		    self.vbox.pack_start(self.hbox_list[2*i], False, False, 6)			# adding H-Separator in V-Box
		    self.vbox.pack_start(self.hbox_list[2*i + 1], False, False, 0)		# adding H-Box containg images and v-separators in V-Box
		
		self.vbox.pack_start(self.hbox_list[10], False, False, 0)			# adding last H-Separator in V-box
		self.vbox.show()
		

	def update_board(self, boardArray) :
		array = boardArray
		for i in range(0,10,1):
			self.vbox.remove(self.hbox_list[i])
		
		self.hbox_list = [self.b]
		for i in range(1,11,1) :
			if(i%2 == 1) :
				self.a = gtk.HBox(False,0)
				self.hbox_list += [self.a]
			else :
				self.b = gtk.HSeparator()
				self.hbox_list += [self.b]
		
        	for i in range(0,5,1):
        		for j in range(0,5,1):
        		        k = (i*5 + j)
        		        if( array[k] == 0):
        		            image_filename = self.blackimage
        		        else:
        		            image_filename = self.whiteimage
		
				self.image = gtk.Image()
				self.image.set_from_file(image_filename)
				self.image.show()
				vseparator = gtk.VSeparator()
				vseparator.show()
			
				self.hbox_list[2*i+1].pack_start(self.image, False, False, 6)		# packing images into H-Boxes
				self.hbox_list[2*i+1].pack_start(vseparator, False, False, 0)		# packing Vseparators into H-Boxes
		
			self.hbox_list[2*i+1].show()							# displays hboxes of vbox
			self.hbox_list[2*i].show()							# displays hseparators of vbox
		
			self.vbox.pack_start(self.hbox_list[2*i], False, False, 0)			# packing Hseparators into V-Box
			self.vbox.pack_start(self.hbox_list[2*i+1], False, False, 6)			# packing H-Boxes into V-Box
		
		self.vbox.pack_start(self.hbox_list[10], False, False, 0)				# adding last H-separator into V-Box
		self.vbox.show()
		
		
	def message(self, msg1, msg2) :
		self.welcome1_label.set_label(msg1)
		self.welcome2_label.set_label(msg2)
		

	def menuplay(self, widget, data = None):
	        self.level=1
	        self.correct=0
	        self.pauseStatus=0
	        self.printboard()
	        self.cor=0.0000
	        self.tot=0.0000
	
	        self.message("Play Deducto","Have fun!!!")
		self.efficiency_value.set_label("?")

	        self.button1.set_label("Yes")
	        self.button2.set_label("No")
	
	        self.playStatus=1
	        self.start=1
	        self.updatepanel()
	
	def resume(self, widget, data = None):
	        self.update_board(self.boardArray)
	        self.pauseStatus=0
	        self.playStatus=1
	        self.message("Game","resumed")
	        self.button1.set_label("Yes")
		self.button2.set_label("No")
	
	
	def opt1(self,widget):
	        if(self.start==0):
	            self.menuplay(self.start)
	        else:
	            if(self.playStatus==0):
	                boardArray = self.genBoard(1)
	                self.boardStatus = 1
	                self.message("Board status","True")
	                self.update_board(boardArray)
	            else:
	                self.tot = int(self.tot) + 1
	                if (int(self.boardStatus) == 1):
	                    self.message("Your answer","Right")        
	                    self.correct = int(self.correct) + 1
	                    self.cor = int(self.cor) + 1
	                    self.score(str(100*(self.cor)/(self.tot)))
	                    if (self.correct == 5):
	                        self.level = self.level + 1
	                        self.correct = 0
	                        if (self.level == 10):
	                            self.playStatus = 0
	                else:
	                    self.score(str(100*(self.cor)/(self.tot)))
	                    self.correct = 0
	                    self.message("Your answer","Wrong")
	                self.printboard()
	            self.updatepanel() 
	
	def opt2(self,widget):
	        if(self.start==0):
	            self.sample()   
	        else:
	            if(self.playStatus==0):
	                boardArray = self.genBoard(0)
	                self.boardStatus = 0
	                self.message("Board status","False")
	                self.update_board(boardArray)
	            else:
	                self.tot= int(self.tot) + 1
	                if (int(self.boardStatus) == 0):    
	                    self.message("Your answer","Right")
	                    self.correct = int(self.correct) + 1
	                    self.cor = int(self.cor) + 1
	                    self.score(str(100*(self.cor)/(self.tot)))
	                    if (self.correct == 5):
	                        self.level = self.level + 1
	                        self.correct = 0
	                        if (self.level == 10):
	                            self.playStatus = 0
	                else:
	                    self.score(str(100*(self.cor)/(self.tot)))
	                    self.message("Your answer","Wrong")
	                    self.correct = 0
	                self.printboard()
	            self.updatepanel()
	
	def sample(self, widget, data = None):
	        self.playStatus=0
	        self.start=1
	        self.pauseStatus=0
	
	        self.temparray=alist
	
	        self.button1.set_label("True board")
	        self.button2.set_label("False board")

	        self.opt1(self.start)
	        self.updatepanel()
	        
	        
	    
	def updatepanel(self):
        	self.level_value.set_label(str(self.level))
        	self.correct_value.set_label(str(self.correct))
	

	def printboard(self):
        	if( random.randrange( 1, 100 ) < 50 ):
        	    boardArray = self.genBoard(0)
        	    self.boardStatus = 0
        	else:
        	    boardArray = self.genBoard(1)
        	    self.boardStatus = 1
        	self.boardArray=boardArray
        	self.update_board(boardArray) 
        	
	
	def score(self,message1):
	        self.efficiency_value.set_label(message1+"%")
	
	

	def genBoard(self,bool):
        	boardArray=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

        	if( self.level == 1 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelOneTrue()
        	    else:
        	        boardArray = self.levelOneFalse()
        	elif( self.level == 2 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelTwoTrue()
        	    else:
        	        boardArray = self.levelTwoFalse()
        	elif( self.level == 3 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelThreeTrue()
        	    else:
        	        boardArray = self.levelThreeFalse()
        	elif( self.level == 4 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelFourTrue()
        	    else:
        	        boardArray = self.levelFourFalse()
        	elif( self.level == 5 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelFiveTrue()
        	    else:
        	        boardArray = self.levelFiveFalse()
	        elif( self.level == 6 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelSixTrue()
        	    else:
        	        boardArray = self.levelSixFalse()
        	elif( self.level == 7 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelSevenTrue()
        	    else:
        	        boardArray = self.levelSevenFalse()
        	elif( self.level == 8 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelEightTrue()
        	    else:
        	        boardArray = self.levelEightFalse()
        	elif( self.level == 9 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelNineTrue()
        	    else:
        	        boardArray = self.levelNineFalse()
        	elif( self.level == 10 ):
        	    if( bool == 1 ):
        	        boardArray = self.levelTenTrue()
        	    else:
        	        boardArray = self.levelTenFalse()
        	else:
        	    boardArray = [1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,0,1,0,0,0,1]
    	
        	return( boardArray )


################################Stages Starts#################################

#	    """First Stage"""

	def levelOneTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # center square is always 0
	        array[12] = 1
	        
	        return( array )
	
	def levelOneFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # center square is always 0
	        array[12] = 0
	        
	        return( array )
	
	
#	    """Second Stage"""
	
	def levelTwoTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # four corners must match
	        array[0] = array[4]
	        array[20] = array[4]
	        array[24] = array[4]
	        return(array)
	
	def levelTwoFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	
	        if( array[0] == array[4] ):
	            array[0] = 1-array[0]
	        
	        return(array)
	
	
	
#	    """Third Stage"""
	
	def levelThreeTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	        # must be less white than black
	       
	        while c < 13:
	            i = int ( random.randrange( 0, 25 ))
	            if( array[i] == 0 ):
	                array[i] = 1
	                c+=1
	        
	        return(array)
	
	
	def levelThreeFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	        # must be less white than black
	       
	        while c > 12:
	            i = int ( random.randrange( 0, 25 ))
	            if( array[i] == 1 ):
	                array[i] = 0
	                c-=1
	        
	        return(array)
	
	
	
#	    """Fourth Stage"""
	def levelFourTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	
	        # must be even black, odd white
	        
	        if( int(c/2)*2 == c ):
	            i = int ( random.randrange( 0, 25 ))
	            array[i] = 1-array[i]
	        
	        return(array)
	
	
	def levelFourFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	
	        # must be even black, odd white
	        
	        if( int(c/2)*2 != c ):
	            i = int ( random.randrange( 0, 25 ))
	            array[i] = 1-array[i]
	        
	        return(array)
	
	
#	    """Fifth Stage"""
	
	def levelFiveTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	        # must be black diagonal
	        if( random.randrange( 0, 100 ) < 50 ):
	            array[0] = 0
	            array[6] = 0
	            array[12] = 0
	            array[18] = 0
	            array[24] = 0
	        else:
	            array[4] = 0
	            array[8] = 0
	            array[12] = 0
	            array[16] = 0
	            array[20] = 0
	        
	        return(array)
	
	def levelFiveFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        c=0
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                c+=1
	
	        # cannot be black diagonal
	       
	        if( array[0] == 0 and array[6] == 0 and array[12] == 0 and array[18] == 0 and array[24] == 0 ):
	            array[6] = 1
	
	        if( array[4] == 0 and array[8] == 0 and array[12] == 0 and array[16] == 0 and array[20] == 0 ):
	            array[16] = 1
	        
	        return(array)
	
#	    """Sixth Stage"""
	
	def levelSixTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # must be white above black
	        
	        for i in range(5,25,1):
	            if( array[i] == 0 ):
	                array[i-5] = 1
	        
	        return(array)
	
	
	def levelSixFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # must be a white to the right of a black
	       
	        for i in range(0,25,5):
	           for j in range(0,4,1):
	                   if( array[i+j] == 0 ):
	                      array[i+j+1] = 1
	                      
	        # check that at least one black has a black above it
	        c = 0;
	        for i in range(0,25,5):
	            if( array[i]==0 and array[i-5]==0 ):
	                c+=1
	        i = 5
	        while( c == 0 ):
	            if( array[i]==0 ):
	                array[i-5] = 0
	                c+=1
	            i+=1
	        
	        return(array)
	
	
#	    """Seventh Stage"""
	
	def levelSevenTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # must be at least one white per row
	        for i in range(0,25,5):
	            c = 0
	            for j in range(0,5,1):
	                c+= array[i+j]
	            if( c == 0 ):
	                array[i+( random.randrange( 0, 5 ))] = 1
	        
	        return(array)
	
	
	def levelSevenFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # must be a row with no white
	        i = random.randrange( 0,5 )
	        for j in range(0,5,1):
	            array[(i*5)+j] = 0
	        
	        return(array)
	
	
#	    """Eighth Stage"""
	
	def levelEightTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,5,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # follow rules
	        for i in range(0,5,1):
	            j = 0
	            if( array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                c = 1
	            elif ( array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                c = 0
	            elif ( array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                c = 1
	            elif ( array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                c = 0
	
	            array[i*5+j] = c
	            for i in range(0,4,1):
	                if(  array[(i-1)*5+j-1] and array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                    c = 0
	            
	            array[i*5+j] = c
	
	            j = 4
	            if(  array[(i-1)*5+j-1] and array[(i-1)*5+j] ):
	                c = 1
	            elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 ):
	                c = 1
	            elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]  ):
	                c = 1
	            elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 ):
	                c = 1
	        
	        return(array)
	
	
	def levelEightFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,5,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        # follow rules
	        for i in range(0,5,1):
	            j = 0
	            if( array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                c = 0
	            elif ( array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                c = 1
	            elif ( array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                c = 0
	            elif ( array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                c = 1
	
	            array[i*5+j] = c
	            for i in range(0,4,1):
	                if(  array[(i-1)*5+j-1] and array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j] and array[(i-1)*5+j+1] ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j] and array[(i-1)*5+j+1]==0 ):
	                    c = 1
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1] ):
	                    c = 0
	                elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 and array[(i-1)*5+j+1]==0 ):
	                    c = 1
	            
	            array[i*5+j] = c
	
	            j = 4
	            if(  array[(i-1)*5+j-1] and array[(i-1)*5+j] ):
	                c = 0
	            elif ( array[(i-1)*5+j-1] and array[(i-1)*5+j]==0 ):
	                c = 0
	            elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]  ):
	                c = 0
	            elif ( array[(i-1)*5+j-1]==0 and array[(i-1)*5+j]==0 ):
	                c = 0
	        
	        return(array)
	
	
#	    """Ninth Stage"""
	
	def levelNineTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	
	        # must be at least white from center to some edge
	        array[12] = 1
	        c = int(random.randrange(0,4))
	        if( c == 0 ):
	            array[2] = 1
	            array[7] = 1 # north
	        elif( c == 1 ):
	            array[13] = 1
	            array[14] = 1 # east
	        elif( c == 2 ):
	            array[17] = 1
	            array[23] = 1 # south
	        else:
	            array[10] = 1
	            array[11] = 1 # west
	        
	        return(array)
	
	
	def levelNineFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	        for i in range(0,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	
	        # cannot be at least white from center to some edge
	        if( array[12] == 1 ):
	            if( array[2] and array[7] ):
	                array[2] = 0
	            if( array[13] and array[14] ):
	                array[13] = 0
	            if( array[17] and array[23] ):
	                array[17] = 0
	            if( array[10] and array[11] ):
	                array[10] = 0
	        
	        return(array)
	
#	    """Tenth Stage"""
	
	def levelTenTrue(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        t=0
	        for i in range(0,10,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                t+=1
	
	        for i in range(10,15,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        b = 0
	       
	        for i in range(15,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                b+=1
	
	        # must be at more white above than below
	        while( t <= b ):
	            c = int(random.randrange(0,10))
	            if( array[c]==0 ):
	                array[c]=1;
	                t+=1;
	
	            if( array[24-c]==1 ):
	                array[24-c]=0;
	                b-=1
	        
	        return(array)
	
	
	def levelTenFalse(self):
	        array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	        t=0
	        for i in range(0,10,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                t+=1
	
	        for i in range(10,15,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	
	        b = 0
	       
	        for i in range(15,25,1):
	            if( random.randrange( 0, 100 ) < 50 ):
	                array[i] = 0
	            else:
	                array[i] = 1
	                b+=1
	        # must be at more white above than below
	        while( t > b ):
	            c = int(random.randrange(0,10))
	            if( array[c]==1 ):
	                array[c]=0;
	                t-=1;
	
	            if( array[24-c]==0 ):
	                array[24-c]=1;
	                b+=1
	        
	        return(array)

	
	################################################################



	def close(self):
		self.game_exit()
	
	def game_exit(self):
		self._logger.debug('Quiting Game')
		self.las.exit_game()
		gtk.main_quit()

	

	def main(self):
		# All PyGTK applications must have a gtk.main(). Control ends here
		# and waits for an event to occur (like a key press or mouse event).
		gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
	gui = Deducto(is_stand_alone = True, base_path = "")

